﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
using Truron.Purchase.Interface;
using Truron.Purchase.Api.Utility;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Truron.Purchase.Model;
using Newtonsoft.Json;
using Truron.Purchase.Utility;

namespace Truron.Purchase.Api.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class Purchase_OuterRequestNoteRelationController : ControllerBase
    {
        private readonly IPurchase_OuterRequestNoteRelationService _purchase_OuterRequestNoteRelationService;
        private readonly IPurchase_OuterRequestDetailService _purchase_OuterRequestDetailService;
        private readonly IPurchase_OuterNoteService _Purchase_OuterNoteService;
        private readonly IPurchase_OuterNoteDetailService _Purchase_OuterNoteDetailService;
        public Purchase_OuterRequestNoteRelationController(IPurchase_OuterRequestNoteRelationService Purchase_OuterRequestNoteRelationService, 
            IPurchase_OuterRequestDetailService Purchase_OuterRequestDetailService, IPurchase_OuterNoteService Purchase_OuterNoteService,
            IPurchase_OuterNoteDetailService Purchase_OuterNoteDetailService)
        {
            _purchase_OuterRequestNoteRelationService = Purchase_OuterRequestNoteRelationService;
            _purchase_OuterRequestDetailService = Purchase_OuterRequestDetailService;
            _Purchase_OuterNoteService = Purchase_OuterNoteService;
            _Purchase_OuterNoteDetailService = Purchase_OuterNoteDetailService;
        }
        public class DeleteByRequestIdAndNoteIdParam
        {
            public int RequestId { get; set; }
            public int NoteId { get; set; }
        }
        public IActionResult DeleteByRequestIdAndNoteId(DeleteByRequestIdAndNoteIdParam param)
        {
            if (param.RequestId == 0||param.NoteId==0)
                return Ok(JsonConvert.SerializeObject(new ResultData(ErrorCodeConst.ParameterError)));
            var userToken = SysCenterServiceHelp.GetUser(HttpContext);
            var resultCode = _purchase_OuterRequestNoteRelationService.DeleteByRequestIdAndNoteId(param.RequestId,param.NoteId, userToken);
            _purchase_OuterRequestDetailService.UpdateStatus(param.RequestId, 1, userToken);
            return Ok(JsonConvert.SerializeObject(new ResultData<object>() { code=0,data = resultCode }));
        }
        public class UpdateExpectMoneyParam
        {
            public int Id { get; set; }
            public decimal ExpectMoney { get; set; }
            public int OuterNum { get; set; }
        }
        public IActionResult UpdateExpectMoney(UpdateExpectMoneyParam param)
        {
            if(param.Id==0)
            {
                return Ok(JsonConvert.SerializeObject(new ResultData(ErrorCodeConst.ParameterError)));
            }
            var relation = _purchase_OuterRequestNoteRelationService.GetDetail(param.Id);
            var Note = _Purchase_OuterNoteService.GetPurchase_OuterNoteDetail(relation.OuterNoteId);
            //
            var RelationList = _purchase_OuterRequestNoteRelationService.GetRelationListByNoteId(Note.Id);
            var sumNum = RelationList.FindAll(x => x.Id != param.Id).Sum(x=>x.OuterNum);
            if(Note.TypeNum<(sumNum + param.OuterNum))
            {
                return Ok(JsonConvert.SerializeObject(new ResultData(ErrorCodeConst.OuterNoteRelationNumError)));
            }

            var userToken = SysCenterServiceHelp.GetUser(HttpContext);
            //重新计算成本
            var singlePrice = Math.Round(Note.Price / Note.TypeNum, 2);
            var ExpectMoney = Math.Round(singlePrice * param.OuterNum,2);
            _purchase_OuterRequestNoteRelationService.UpdateExpectMoney(param.Id, ExpectMoney, param.OuterNum, userToken);
            return Ok(JsonConvert.SerializeObject(new ResultData<object>() { code = 0 }));
        }
        public class UpdateRelationNumParam
        {
            public int RequestId { get; set; }
            public int NoteId { get; set; }
            public int Num { get; set; }
        }
        public IActionResult UpdateRelationNum(UpdateRelationNumParam param)
        {
            var userToken = SysCenterServiceHelp.GetUser(HttpContext);
            //先看下除去本次关联，改委外单还剩余多少数量
            Purchase_OuterNoteDetail NoteDetail = _Purchase_OuterNoteDetailService.GetEntity(param.NoteId);
            List<Purchase_OuterRequestNoteRelation> RelationList = _purchase_OuterRequestNoteRelationService.GetListByNoteId(param.NoteId);
            int UsedNum = RelationList.FindAll(x => x.OuterNoteId != param.NoteId).Sum(x=>x.OuterNum);
            int ResetNum = NoteDetail.TypeNum - UsedNum;
            if(ResetNum<param.Num)
            {
                return Ok(JsonConvert.SerializeObject(new ResultData<object>() { code = -1 }));
            }
            else
            {
                Purchase_OuterRequestNoteRelation relationEntity = _purchase_OuterRequestNoteRelationService.GetDetailByRequestIdAndNoteId(param.RequestId,param.NoteId);
                relationEntity.OuterNum = param.Num;
                relationEntity.ExpectMoney = NoteDetail.PriceNum / NoteDetail.TypeNum * relationEntity.OuterNum;
                _purchase_OuterRequestNoteRelationService.Save(relationEntity, userToken);
                return Ok(JsonConvert.SerializeObject(new ResultData<object>() { code = 0 }));
            }

        }
    }
}
